#!/usr/bin/env python
# encoding: utf-8
'''
@author: 王震
@license: 四川凡狼科技有限公司
@contact: 2668985108@qq.com
@file: resources.py
@time: 2022/1/13 9:55 下午
@desc:
'''
from flask import request, Blueprint
from app.business.resources.resources import *
from app.public.sqlMethods import auto

resources = Blueprint(name='resources', import_name='__name__', url_prefix='/api')


@resources.route('/add/resources', methods=['POST'])
@auto.login_required
def add_resources() -> dict:
    """
    创建资源标签
    :return: dict
    """
    data = request.json
    return create_resources(name=data['name'])


@resources.route('/get/resources', methods=['GET'])
@auto.login_required
def get_resources() -> list:
    """
    获取管理系统标签列表
    :return: list
    """
    return get_system_resources()


@resources.route('/customer/resources', methods=['GET'])
def customer_resources() -> list:
    """
    获取客户端标签列表
    :return: list
    """
    return get_customer_resources()


@resources.route('/add/resources/title', methods=['POST'])
@auto.login_required
def create_resources() -> dict:
    """
    创建资源类型
    :return: dict
    """
    data = request.json
    return add_resources_title(name=data['name'])


@resources.route('/resources/client', methods=['GET'])
def resources_list() -> list:
    """
    客户端获取资源类型列表
    :return:  list
    """
    return client_resources_list()


@resources.route('/get/system/resources', methods=['GET'])
@auto.login_required
def get_system_resources_list() -> list:
    """
    管理系统获取资源类型列表
    :return: list
    """
    page_number = request.args.get('pageNumber', 1)
    page_size = request.args.get('pageSize', 10)
    return system_resources(page_number=page_number, page_size=page_size)


@resources.route('/edit/resources', methods=['PUT'])
@auto.login_required
def _edit_resources():
    """
    编辑资源类型
    :return:
    """
    data = request.json
    return edit_resources(data=data)


@resources.route('/del/resources', methods=['DELETE'])
@auto.login_required
def _del_resources():
    """
    删除资源类型
    :return:
    """
    data = request.json
    id = data['id']
    return delete_resources(id=id)


@resources.route('/create/resources/works', methods=['POST'])
@auto.login_required
def create_works() -> dict:
    """
    创建资源作品
    :return:dict
    """
    data = request.json
    return create_resources_works(data=data)


@resources.route('/resources/works', methods=['GET'])
def resources_works() -> list:
    """
    获取客户端资源列表
    :return: list
    """
    page_number = request.args.get('pageNumber', 1)
    page_size = request.args.get('pageSize', 10)
    id = request.args.get('id', None)
    return get_resources_works(page_number=page_number, page_size=page_size, id=id)


@resources.route('/resources/works/list', methods=['GET'])
@auto.login_required
def resources_system() -> list:
    """
    管理系统获取资源列表
    :return: list
    """
    page_number = request.args.get('pageNumber', 1)
    page_size = request.args.get('pageSize', 10)
    return resources_system_list(page_number=page_number, page_size=page_size)


@resources.route('/add/fabulous', methods=['POST'])
@auto.login_required
def add_fabulous_resources() -> dict:
    """
    资源点赞
    :return:
    """
    data = request.json
    return add_fabulous(id=data['id'])


@resources.route('/cancel/fabulous', methods=['PUT'])
@auto.login_required
def cancel_fabulous_resources():
    """
    取消点赞
    :return:
    """
    data = request.json
    return cancel_fabulous(id=data['id'])


@resources.route('/query/fabulous', methods=['GET'])
@auto.login_required
def query_fabulous_resources():
    """
    查看当前资源是否被用户收藏过
    :return:
    """
    id = request.args.get('id')
    return query_fabulous(id=id)


@resources.route('/add/collection', methods=['POST'])
@auto.login_required
def add_user_collection():
    """
    加入收藏
    :return:
    """
    data = request.json
    id = data['id'] if 'id' in data.keys() else None
    return add_collection(id=id)


@resources.route('/user/cancel/collection', methods=['POST'])
@auto.login_required
def user_cancel_collection():
    """
    取消收藏
    :return:
    """
    data = request.json
    id = data['id'] if 'id' in data.keys() else None
    return cancel_collection(id=id)


@resources.route('/query/collection', methods=['GET'])
@auto.login_required
def user_query_collection():
    """
    查看当前资源是否加入了收藏
    :return:
    """
    id = request.args.get('id')
    return query_collection(id=id)


@resources.route('/get/resource/details', methods=['GET'])
def get_resource_work_details():
    """
    获取资源详情
    :return:
    """
    id = request.args.get('id')
    return get_resource_details(id=id)


@resources.route('/get/collection/list', methods=['GET'])
@auto.login_required
def get_collection_list():
    """
    获取个人收藏列表
    :return:
    """
    page_number = request.args.get('pageNumber', 1)
    page_size = request.args.get('pageSize', 10)
    return get_collection(page_number=page_number, page_size=page_size)
